﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum DataMessage
{
    //INST_DATA_ATTRIBUTE_UPDATE = 0,//实例属性变更,武者,装备,宝物
    NET_BROKEN = 1,                //网络断线
    NET_CONNECT_COMPLETE = 2,     //网络重连完成
    RED_POINT = 3,                //红点信息
    AREA_ENTER = 4,               //进入区域
    NPC_CLICK = 5,                //点击StoryNPC
    ROOM_SELF_CREATED = 6,       //场景加载完成
    PLAYER_PRISON_REFRESH=7,      //监狱状态变更
}

public delegate void DataMessageHandler(object factor);

public static class DataMessageTool 
{
    private static Dictionary<DataMessage, DataMessageHandler> handlers = new Dictionary<DataMessage,DataMessageHandler>();
    public static void AddHandler(DataMessage messageType, DataMessageHandler handler)
    {
        if (handlers.ContainsKey(messageType))
        {
            handlers[messageType] += handler;
        }
        else
        {
            handlers[messageType] = handler;
        }
    }

    public static void RemoveHandler(DataMessage messageType, DataMessageHandler handler)
    {
        if (handlers.ContainsKey(messageType))
        {
            handlers[messageType] -= handler;
        }
    }

    public static void AddMessage(DataMessage message, object factor)
    {
        Debug.LogWarning("new message " + message);
        if (handlers.ContainsKey(message) && handlers[message] != null)
        {
            handlers[message](factor);
        }
        else
        {
            Debug.LogError(message.ToString() + " dont have handler");
        }
    }
    
    public static void CleanAllHandler ()
    {
		handlers = new Dictionary<DataMessage,DataMessageHandler>();
    }
}
